package BinaryTree;

public class TestBinaryTree {

    public static void main(String[] args) {
        // 初始化二叉树并插入节点
        int[] aa = {1, 2, 3, 4, 5};
        int[] bb = { 6, 7, 8, 9, 10};
        BinaryTree<Integer> tree = new BinaryTree<>(0);
        BinaryTreeNode<Integer> node = tree.root;

        for (int i = 0; i < aa.length; i++) {
            tree.insertLeft(aa[i], node);
            tree.insertRight(bb[i], node);
        }

        // 1. 输出原始二叉树（前序遍历）
        System.out.println("原始二叉树:");
        tree.traversal(0);

        // 2. 输出二叉树的所有叶子节点
        System.out.println("\n二叉树的所有叶子节点:");
        tree.yezi(tree.root);

        // 3. 统计节点数量
        System.out.println("\n节点的数量为: " + tree.count(tree.root));

        // 4. 交换二叉树任意两个节点（假设二叉树的每个节点的值都不一样）
        BinaryTreeNode<Integer> node1 = tree.search(tree.root, 3);
        BinaryTreeNode<Integer> node2 = tree.search(tree.root, 7);
        int temp = node1.getData();
        node1.setData(node2.getData());
        node2.setData(temp);
        System.out.print("节点3和节点7交换后: ");
        tree.traversal(0);
        System.out.println();
        // 5. 将现有二叉树的某个子树a移到其他子树b中
        BinaryTreeNode<Integer> a = tree.search(tree.root, 2);
        BinaryTreeNode<Integer> b = tree.search(tree.root, 3);

        // 5.1 查找a的双亲
        BinaryTreeNode<Integer> parentOfa = tree.search2(tree.root, a.getData());
        System.out.println("a的双亲:" + parentOfa.getData());

        // 插入操作将a插入到b中
        tree.insertl(a, b);

        // 将子树a置空
        if (parentOfa.lChild != null && parentOfa.lChild.getData().equals(a.getData())) {
            parentOfa.lChild = null;
        } else if (parentOfa.rChild != null && parentOfa.rChild.getData().equals(a.getData())) {
            parentOfa.rChild = null;
        }

        // 遍历一下，检验是否正确
        System.out.print("将a移到b以后:");
        tree.traversal(0);
        System.out.println();
        // 6. 删除一个节点
        BinaryTreeNode<Integer> delete = tree.search(tree.root, 3);

        // 找到这个节点的双亲节点
        BinaryTreeNode<Integer> parentOfdelete = tree.search2(tree.root, delete.getData());
        if (parentOfdelete.lChild != null && parentOfdelete.lChild.getData().equals(delete.getData())) {
            parentOfdelete.lChild = null;
        } else if (parentOfdelete.rChild != null && parentOfdelete.rChild.getData().equals(delete.getData())) {
            parentOfdelete.rChild = null;
        }

        // 找到节点的左孩子和右孩子
        BinaryTreeNode<Integer> left = delete.lChild;
        BinaryTreeNode<Integer> right = delete.rChild;

        // 插入操作
        if (left != null) {
            tree.insertl(left, parentOfdelete);
        }
        if (right != null) {
            tree.insertr(right, parentOfdelete);
        }

        // 遍历一下，检验是否正确
        System.out.print("删除节点以后:");
        tree.traversal(0);
        System.out.println();
        // 7. 缩进结构打印
        System.out.println("缩进结构打印:");
        tree.suojing(tree.root, 0);
    }
}

